package com.srwl.mytx.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.bm.library.Info;
import com.srwl.mytx.R;
import com.srwl.mytx.adapter.BigPictureAdapter;
import com.srwl.mytx.adapter.ChatFileShowAdapter;
import com.srwl.mytx.domain.Message;
import com.srwl.mytx.domain.MyFile;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jzvd.JzvdStd;

/**
 * 大屏幕展示聊天页面的图片或者视频
 */
public class ChatFileShowPageView extends FrameLayout implements ChatFileShowAdapter.InteractionListener {
    @BindView(R.id.pager_big_picture)
    ViewPager vp_bigPicture;
    @BindView(R.id.fl_bigPicture)
    View fl_bigPicture;
    @BindView(R.id.img_bg_black)
    ImageView iv_background;
    private ChatFileShowAdapter bigPictureAdapter;
    private Context context;
    private AlphaAnimation in = new AlphaAnimation(0, 1);
    private AlphaAnimation out = new AlphaAnimation(1, 0);
    private InteractionListener listener;
    private List<Message> myFileList = new ArrayList<>();
    private List<String> imagePathsCover = new ArrayList<>();
    private Info info;
    private boolean isShowAnimate;
    private boolean hadInit;
    private boolean isShowing;
    private boolean hasNotch;

    public ChatFileShowPageView(@NonNull Context context) {
        super(context);
    }

    public ChatFileShowPageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        inflate(context, R.layout.frame_layout_big_image, this);
        ButterKnife.bind(this);
        //大图黑色背景显示隐藏 持续时间
        in.setDuration(400);
        in.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

                iv_background.setVisibility(View.VISIBLE);
                bigPictureAdapter.showAnimateIn(info);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        out.setDuration(400);
        out.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iv_background.setVisibility(View.INVISIBLE);
                setVisibility(GONE);
                isShowAnimate = false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        initViewPage();
    }

    private void initViewPage() {
        vp_bigPicture.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (listener != null) {
                    postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            listener.onPageChange(position);
                        }
                    }, 400);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void init(List<Message> myFileList, boolean hasNotch, InteractionListener listener) {
        hadInit = true;
        this.hasNotch = hasNotch;
        this.myFileList = myFileList;
        // imagePathsCover.addAll(myFileList);
        this.listener = listener;
        bigPictureAdapter = new ChatFileShowAdapter(context, myFileList, this);
        vp_bigPicture.setAdapter(bigPictureAdapter);
    }


    public void showBigPictures(int currentItemPosition, Info info) {
        if (!hasNotch) {
            //让状态栏消失
            ((Activity) context).getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        isShowing = true;
        this.info = info;
        bigPictureAdapter.setAnimationPosition(currentItemPosition, info);
        // imagePathsCover.addAll(myFileList);
        bigPictureAdapter.notifyDataSetChanged();
        vp_bigPicture.setCurrentItem(currentItemPosition, false);
        iv_background.startAnimation(in);
        setVisibility(VISIBLE);
    }


    public void hideBigPictures(Info info) {
        if (!hasNotch) {
            //让状态栏消失
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//让状态栏显示回来
        }
        isShowAnimate = true;
        this.info = info;
        bigPictureAdapter.showAnimateOut(info);
        iv_background.startAnimation(out);
        isShowing = false;
    }

    public void refresh() {
        bigPictureAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(int position) {
        Info info = listener.onGetImageRectInfo(position);
        hideBigPictures(info);
    }

    @Override
    public void onItemRemove(int position) {

    }

    public int getCurrentItem() {
        return vp_bigPicture.getCurrentItem();
    }

    public boolean isShowAnimate() {
        return isShowAnimate;
    }


    public boolean onBackPressed() {
        if (isShowing) {
            Info info = listener.onGetImageRectInfo(vp_bigPicture.getCurrentItem());
            hideBigPictures(info);
            return false;
        } else {
            return true;
        }
    }

    public boolean isInit() {
        return hadInit;
    }


    public interface InteractionListener {
        void onPageChange(int position);

        Info onGetImageRectInfo(int position);
    }
}
